package com.example.test.dialogfactory;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout.OnRefreshListener;
import easier.recycler.DataViewHolder;
import easier.recycler.SingleTypeAdapter;
import java.util.ArrayList;
import java.util.List;

public class MainActivity34 extends AppCompatActivity {

    private SwipeRefreshLayout mRefresh;
    private RecyclerView mRecycler;

    public static void start( Context context ) {

        Intent starter = new Intent( context, MainActivity34.class );
        context.startActivity( starter );
    }

    @Override
    protected void onCreate( Bundle savedInstanceState ) {

        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_main34 );
        initView();
        initViewAction();
    }

    private void initView() {

        mRefresh = (SwipeRefreshLayout) findViewById( R.id.refresh );
        mRecycler = (RecyclerView) findViewById( R.id.recycler );
    }

    private void initViewAction() {

        List< String > data = new ArrayList<>();
        for ( int i = 0; i < 100; i++ ) {
            data.add( "item: " + i );
        }

        mRecycler.setLayoutManager( new LinearLayoutManager( this ) );
        mRecycler.setAdapter( new SingleTypeAdapter< String >( data ) {
            @Override
            protected DataViewHolder< String > buildHolder( int position, String o ) {

                return new DataViewHolder<>( R.layout.recycler_item_01, o );
            }
        } );

        mRefresh.setOnRefreshListener( new OnRefreshListener() {
            @Override
            public void onRefresh() {

                mRefresh.postDelayed( new Runnable() {
                    @Override
                    public void run() {

                        mRefresh.setRefreshing( false );
                    }
                }, 3000 );
            }
        } );
    }
}